home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / PixelPlotTest / PixelPlotTest.p < prev    next >
Text File  |  1994-12-03  |  4KB  |  132 lines

  1. program PixelPlotTest;
  2.  
  3.     const
  4.         kSizeH = 300;
  5.         kSizeV = 40;
  6.     var
  7.         w: WindowPtr;
  8.         c, saveColor: RGBColor;
  9.         startTime, endTime: LongInt;
  10.         h, v, i: Integer;
  11.         r: Rect;
  12.         rowBytes: Longint;
  13.         pixAddr: Ptr;
  14.         baseAddr: Longint;
  15.         rowStartTable: array[0..kSizeV] of Longint;
  16.         mmuMode: SignedByte;
  17. begin
  18.  
  19.     SetRect(r, 100, 100, 400, 301);
  20.     w := NewCWindow(nil, r, 'Pixel Plotting Speed Test', true, 2, WindowPtr(-1), false, 0);
  21.     SetPort(w);
  22.  
  23.     GetForeColor(saveColor);
  24.  
  25.     c.red := $ffff;
  26.     c.green := 0;
  27.     c.blue := $ffff;
  28.     startTime := TickCount;
  29.     for h := 0 to kSizeH do
  30.         for v := 0 to kSizeV do
  31.             SetCPixel(h, v, c);
  32.     endTime := TickCount;
  33.  
  34.     MoveTo(10, kSizeV + 20);
  35.     DrawString(stringof('SetCPixel test: ', (endTime - startTime) : 1, ' ticks.'));
  36.  
  37.     c.red := $ffff;
  38.     c.green := $ffff;
  39.     c.blue := 0;
  40.     RGBForeColor(c);
  41.     startTime := TickCount;
  42.     for h := 0 to kSizeH do
  43.         for v := 0 to kSizeV do
  44.             begin
  45.                 RGBForeColor(c);
  46.                 MoveTo(h, v);
  47.                 Line(0, 0);
  48.             end;
  49.     endTime := TickCount;
  50.  
  51.     RGBForeColor(saveColor);
  52.     MoveTo(10, kSizeV + 40);
  53.     DrawString(stringof('MoveTo/LineTo test: ', (endTime - startTime) : 1, ' ticks.'));
  54.  
  55.     c.red := 0;
  56.     c.green := $ffff;
  57.     c.blue := $ffff;
  58.     RGBForeColor(c);
  59.     startTime := TickCount;
  60.     for h := 0 to kSizeH do
  61.         for v := 0 to kSizeV do
  62.             begin
  63.                 RGBForeColor(c);
  64.                 SetRect(r, h, v, h + 1, v + 1);
  65.                 PaintRect(r);
  66.             end;
  67.     endTime := TickCount;
  68.  
  69.     RGBForeColor(saveColor);
  70.     MoveTo(10, kSizeV + 60);
  71.     DrawString(stringof('SetRect/PaintRect test: ', (endTime - startTime) : 1, ' ticks.'));
  72.  
  73.     if GetMainDevice^^.gdPMap^^.pixelSize <> 8 then
  74.         begin
  75.             MoveTo(10, kSizeV + 80);
  76.             DrawString('Direct plotting is only supported in 256 colors/grays.');
  77.         end
  78.     else
  79.         begin
  80.  
  81.             mmuMode := true32b;
  82.  
  83. {CWindowPtr(w)^.portPixMap^^.bounds.top;}
  84. {CWindowPtr(w)^.portPixMap^^.bounds.left;}
  85.             rowBytes := BitAnd(GetMainDevice^^.gdPMap^^.rowBytes, $3fff);
  86.             SetRect(r, 0, 0, kSizeH, kSizeV);
  87.             ShieldCursor(r, CWindowPtr(w)^.portPixMap^^.bounds.topLeft);
  88.             baseAddr := Longint(GetMainDevice^^.gdPMap^^.baseAddr);
  89.  
  90.             startTime := TickCount;
  91.             SwapMMUMode(mmuMode);
  92.             for i := 1 to 10 do
  93.                 for h := 0 to kSizeH do
  94.                     for v := 0 to kSizeV do
  95.                         begin
  96.                             pixAddr := Ptr(baseAddr + rowBytes * (v + 100) + h + 100);
  97.                             pixAddr^ := $70 + i; {Some pixel value. We can get this through the color table.}
  98.                         end;
  99.             SwapMMUMode(mmuMode);
  100.             endTime := TickCount;
  101.  
  102.             ShowCursor;
  103.             MoveTo(10, kSizeV + 80);
  104.             DrawString(stringof('Direct plotting test: ', (endTime - startTime) : 1, ' ticks/10 times.'));
  105.  
  106. {Optimize by precalculating as much as possible:}
  107.             for v := 0 to kSizeV do
  108.                 rowStartTable[v] := baseAddr + rowBytes * (v + 100) + 100;
  109.             ShieldCursor(r, CWindowPtr(w)^.portPixMap^^.bounds.topLeft);        {Protect us from damaging the cursor}
  110.             startTime := TickCount;
  111.             SwapMMUMode(mmuMode);                                                {We must be in 32-bit mode}
  112.             for i := 1 to 10 do
  113.                 for h := 0 to kSizeH do
  114.                     for v := 0 to kSizeV do
  115.                         begin
  116.                             pixAddr := Ptr(rowStartTable[v] + h);
  117.                             pixAddr^ := $60 + i; {Some pixel value. We can get this through the color table.}
  118.                         end;
  119.             SwapMMUMode(mmuMode);                                                {Back to previous addressing mode}
  120.             endTime := TickCount;
  121.             ShowCursor;                                                                {Balances ShieldCursor}
  122.             MoveTo(10, kSizeV + 100);
  123.             DrawString(stringof('Opt. direct plotting test: ', (endTime - startTime) : 1, ' ticks/10 times.'));
  124.  
  125.         end;
  126.  
  127.     MoveTo(10, kSizeV + 120);
  128.     DrawString('Click mouse to exit.');
  129.     while not Button do
  130.         ;
  131.  
  132. end.